package com.funquer.cms.controller;

import com.alibaba.fastjson.JSON;
import com.funquer.admin.pageModel.Json;
import com.funquer.cms.model.ArctypeEntity;
import com.funquer.cms.pageModel.ArctypePage;
import com.funquer.cms.service.ArctypeServiceI;
import com.funquer.util.ExceptionUtil;
import com.funquer.util.ResourceUtil;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: Administrator
 * Date: 13-3-19
 * Time: 上午11:33
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/arctypeController")
public class ArctypeController {

    private ArctypeServiceI arctypeService;

    public ArctypeServiceI getArctypeService() {
        return arctypeService;
    }

    @Autowired
    public void setArctypeService(ArctypeServiceI arctypeService) {
        this.arctypeService = arctypeService;
    }

    @RequestMapping("/index")
    public String index(){
        return "cms/arctype/index";
    }

    @RequestMapping("/treegrid")
    @ResponseBody
    public List<ArctypePage> treegrid(ArctypePage arctypePage){
        return arctypeService.treegrid();
    }

    @RequestMapping("/toAdd")
    public String toAdd(){
        return "cms/arctype/add";
    }


    @RequestMapping("/toAddChild")
    public String toAddChild(String parentID, HttpServletRequest request){
        request.setAttribute("parentArcType",parentID);
        return "cms/arctype/add";
    }


    @RequestMapping("/add")
    @ResponseBody
    public Json add(ArctypeEntity arctypeEntity) {
        Json j = new Json();
        try {
            ArctypeEntity r = arctypeService.save(arctypeEntity);
            j.setSuccess(true);
            j.setMsg("添加成功！");
            j.setObj(r);
        } catch (Exception e) {
            j.setMsg(e.getMessage());
        }
        return j;
    }




    @RequestMapping("/upload")
    public void upload(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
        String savePath = session.getServletContext().getRealPath("/") + ResourceUtil.getUploadDirectory() + "/";// 文件保存目录路径
        String saveUrl = "/" + ResourceUtil.getUploadDirectory() + "/";// 要返回给xhEditor的文件保存目录URL

        SimpleDateFormat yearDf = new SimpleDateFormat("yyyy");
        SimpleDateFormat monthDf = new SimpleDateFormat("MM");
        SimpleDateFormat dateDf = new SimpleDateFormat("dd");
        Date date = new Date();
        String ymd = yearDf.format(date) + "/" + monthDf.format(date) + "/" + dateDf.format(date) + "/";
        savePath += ymd;
        saveUrl += ymd;

        File uploadDir = new File(savePath);// 创建要上传文件到指定的目录
        if (!uploadDir.exists()) {
            uploadDir.mkdirs();
        }

        String contentDisposition = request.getHeader("Content-Disposition");// 如果是HTML5上传文件，那么这里有相应头的
        if (contentDisposition != null) {// HTML5拖拽上传文件
            Long fileSize = Long.valueOf(request.getHeader("Content-Length"));// 上传的文件大小
            String fileName = contentDisposition.substring(contentDisposition.lastIndexOf("filename=\""));// 文件名称
            fileName = fileName.substring(fileName.indexOf("\"") + 1);
            fileName = fileName.substring(0, fileName.indexOf("\""));

            ServletInputStream inputStream;
            try {
                inputStream = request.getInputStream();
            } catch (IOException e) {
                this.uploadError("上传文件出错！", response);
                ExceptionUtil.getExceptionMessage(e);
                return;
            }

            if (inputStream == null) {
                this.uploadError("您没有上传任何文件！", response);
                return;
            }

            if (fileSize > ResourceUtil.getUploadFileMaxSize()) {
                this.uploadError("上传文件超出限制大小！", fileName, response);
                return;
            }

            // 检查文件扩展名
            String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
            if (!Arrays.<String> asList(ResourceUtil.getUploadFileExts().split(",")).contains(fileExt)) {
                this.uploadError("上传文件扩展名是不允许的扩展名。\n只允许" + ResourceUtil.getUploadFileExts() + "格式！", response);
                return;
            }

            String newFileName = UUID.randomUUID().toString().replaceAll("-", "") + "." + fileExt;// 新的文件名称
            File uploadedFile = new File(savePath, newFileName);

            try {
                FileCopyUtils.copy(inputStream, new FileOutputStream(uploadedFile));
            } catch (FileNotFoundException e) {
                this.uploadError("上传文件出错！", response);
                ExceptionUtil.getExceptionMessage(e);
                return;
            } catch (IOException e) {
                this.uploadError("上传文件出错！", response);
                ExceptionUtil.getExceptionMessage(e);
                return;
            }

            this.uploadSuccess(request.getContextPath() + saveUrl + newFileName, fileName, 0, response);// 文件上传成功
            return;
        } else {// 不是HTML5拖拽上传,普通上传
            if (ServletFileUpload.isMultipartContent(request)) {// 判断表单是否存在enctype="multipart/form-data"
                DiskFileItemFactory dfif = new DiskFileItemFactory();
                dfif.setSizeThreshold(1 * 1024 * 1024);// 设定当上传文件超过1M时，将产生临时文件用于缓冲
                dfif.setRepository(new File(savePath));// 存放临时文件的目录
                ServletFileUpload sfu = new ServletFileUpload(dfif);
                try {
                    List<FileItem> fis = sfu.parseRequest(request);
                    for (FileItem fi : fis) {
                        if (fi.isFormField()) {
                            // 是表单项，不是上传项
                        } else {
                            String fileName = fi.getName();// 上传的文件名称，如果是欧鹏浏览器，这个会带路径

                            if (fi.getSize() > ResourceUtil.getUploadFileMaxSize()) {
                                this.uploadError("上传文件超出限制大小！", fileName, response);
                                return;
                            }

                            try {
                                // 检查文件扩展名
                                String fileExt = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
                                if (!Arrays.<String> asList(ResourceUtil.getUploadFileExts().split(",")).contains(fileExt)) {
                                    this.uploadError("上传文件扩展名是不允许的扩展名。\n只允许" + ResourceUtil.getUploadFileExts() + "格式！", response);
                                    return;
                                }
                                String newFileName = UUID.randomUUID().toString().replaceAll("-", "") + "." + fileExt;// 新的文件名称
                                fi.write(new File(savePath, newFileName));

                                this.uploadSuccess(request.getContextPath() + saveUrl + newFileName, fileName, 0, response);// 文件上传成功
                                return;
                            } catch (Exception e) {
                                this.uploadError("上传文件出错！", response);
                                ExceptionUtil.getExceptionMessage(e);
                                return;
                            }
                        }
                    }
                } catch (FileUploadException e) {
                    this.uploadError("上传文件出错！", response);
                    ExceptionUtil.getExceptionMessage(e);
                    return;
                }
            } else {
                // 不是multipart/form-data表单
                this.uploadError("您没有上传任何文件！", response);
                return;
            }
            return;
        }

    }


    private void uploadError(String err, String msg, HttpServletResponse response) {
        Map<String, Object> m = new HashMap<String, Object>();
        m.put("err", err);
        m.put("msg", msg);
        this.writeJson(m, response);
    }

    private void uploadError(String err, HttpServletResponse response) {
        this.uploadError(err, "", response);
    }

    private void uploadSuccess(String newFileName, String fileName, int id, HttpServletResponse response) {
        Map<String, Object> m = new HashMap<String, Object>();
        m.put("err", "");
        Map<String, Object> nm = new HashMap<String, Object>();
        nm.put("url", newFileName);
        nm.put("localfile", fileName);
        nm.put("id", id);
        m.put("msg", nm);
        this.writeJson(m, response);
    }


    private void writeJson(Object object, HttpServletResponse response) {
        try {
            String json = JSON.toJSONStringWithDateFormat(object, "yyyy-MM-dd HH:mm:ss");
            response.setContentType("text/html;charset=utf-8");
            response.getWriter().write(json);
            response.getWriter().flush();
            response.getWriter().close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @RequestMapping("/toEdit")
    public String toEdit(String id, HttpServletRequest request){
        ArctypeEntity entity= arctypeService.getOne(id);
        request.setAttribute("entity",entity);
        return "cms/arctype/edit";
    }

    @RequestMapping("/edit")
    @ResponseBody
    public Json edit(ArctypeEntity arctypeEntity) {
        Json j = new Json();
        try {
            ArctypeEntity r = arctypeService.edit(arctypeEntity);
            j.setSuccess(true);
            j.setMsg("编辑成功！");
            j.setObj(r);
        } catch (Exception e) {
            j.setMsg(e.getMessage());
        }
        return j;
    }


    @RequestMapping("/remove")
    @ResponseBody
    public Json remove(ArctypeEntity arctypeEntity){
        arctypeService.remove(arctypeEntity.getId());
        Json j = new Json();
        j.setSuccess(true);
        j.setObj(arctypeEntity.getId());
        j.setMsg("删除成功！");
        return j;
    }

}
